package com.partners.configure;

import com.partners.model.Member;
import com.partners.model.TokenCodeModel;
import com.partners.utils.TimestampUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

import static com.partners.constant.CacheTopic.TOKEN;

/**
 * 缓存token
 * @author liangyongle
 * @date 2019-01-14
 */
@Component
public class TokenConfig {
    @Cacheable(value = TOKEN,key = "#fansInfo.getToken()")
    public TokenCodeModel createAndSaveToken(Member fansInfo){
        TokenCodeModel tokenCodeModel = new TokenCodeModel();
        tokenCodeModel.setId(fansInfo.getId());
        tokenCodeModel.setName(fansInfo.getName());
        tokenCodeModel.setToken(fansInfo.getToken());
        tokenCodeModel.setOpenId(fansInfo.getOpenId());
        tokenCodeModel.setOverTime(TimestampUtils.timestamp(LocalDateTime.now().plusMinutes(30L)));
        return tokenCodeModel;
    }

    @Cacheable(value = TOKEN,key = "#member.getToken()")
    public TokenCodeModel cleanAndSaveToken(Member member){
        TokenCodeModel tokenCodeModel = new TokenCodeModel();
        tokenCodeModel.setId(member.getId());
        tokenCodeModel.setName(member.getName());
        tokenCodeModel.setToken(member.getToken());
        tokenCodeModel.setOpenId(member.getOpenId());
        tokenCodeModel.setOverTime(TimestampUtils.timestamp(LocalDateTime.now().plusMinutes(15L)));
        return tokenCodeModel;
    }



}
